static DIV_LIST: [i64; 7] = [2, 3, 5, 7, 11, 13, 17];

/// [Sub-string divisibility](https://projecteuler.net/problem=43)
fn main() {
    let mut stat = vec![-1; 10];
    let mut result = vec![];
    for a in 1..=9 {
        stat[0] = a;
        generate_number(1, &mut stat, &mut result);
        stat[0] = -1;
    }
    println!("{:#?}", result);
    println!("{:#?}", result.iter().fold(0, |s, x| s + x));
}

fn generate_number(digit: usize, stat: &mut Vec<i64>, result: &mut Vec<i64>) {
    for x in 0..=9 {
        if !stat.contains(&x) {
            stat[digit] = x;
            if digit >= 3 {
                let num = stat[digit - 2] * 100 + stat[digit - 1] * 10 + x;
                if num % DIV_LIST[digit - 3] == 0 {
                    if digit >= 9 {
                        result.push(stat.iter().fold(0, |s, x| s * 10 + x));
                        stat[digit] = -1;
                        break;
                    } else {
                        generate_number(digit + 1, stat, result);
                    }
                }
            } else {
                generate_number(digit + 1, stat, result);
            }
            stat[digit] = -1;
        }
    }
}
